package com.whfc.iot.fse.lift.codec;

import com.whfc.iot.fse.lift.entity.LiftMsg;
import com.whfc.iot.fse.lift.util.LiftMsgUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * 升降机消息编码器
 */
public class LiftMsgEncoder extends MessageToMessageEncoder<LiftMsg> {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, LiftMsg msg, List<Object> out) throws Exception {

        ByteBuf buf = Unpooled.buffer(msg.getLength() + 20);

        LiftMsgUtil.encode(buf, msg);

        //发送
        logger.info("发送数据:{}", ByteBufUtil.prettyHexDump(buf));
        out.add(buf);
    }
}
